{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "eb65abfe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 25)\n",
    "end_date = datetime(2023, 12, 25)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "# 循环遍历日期范围\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    # 格式化日期\n",
    "    formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "    # 循环遍历每个子文件夹\n",
    "    for subfolder in os.scandir(parent_folder):\n",
    "        if subfolder.is_dir():\n",
    "            # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "            item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "            # 构建链接\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v3/new/excel/item/source/detail/v3&order=desc&orderBy=uv&dateType=day&dateRange={formatted_date}|{formatted_date}&itemId={item_id_variable}&device=2&pageId=23.s1150&pPageId=30&pageLevel=2&childPageType=se_keyword&belong=all\"\n",
    "\n",
    "            # 在默认浏览器中打开链接\n",
    "            webbrowser.open(url)\n",
    "\n",
    "    # 增加一天\n",
    "    current_date += timedelta(days=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6a1c0558",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Moved file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\744047791585_2023-12-26_data.xlsx\n",
      "Moved file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\744047791585_2023-12-26_data.xlsx\n",
      "Moved file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\744047791585_2023-12-26_data.xlsx\n"
     ]
    }
   ],
   "source": [
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "import shutil\n",
    "import time\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 26)\n",
    "end_date = datetime(2023, 12, 26)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "\n",
    "# 循环遍历日期范围\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    # 格式化日期\n",
    "    formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "    # 循环遍历每个子文件夹\n",
    "    for subfolder in os.scandir(parent_folder):\n",
    "        if subfolder.is_dir():\n",
    "            # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "            item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "            # 构建链接\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v3/new/excel/item/source/detail/v3&order=desc&orderBy=uv&dateType=day&dateRange={formatted_date}|{formatted_date}&itemId={item_id_variable}&device=2&pageId=23.s1150&pPageId=30&pageLevel=2&childPageType=se_keyword&belong=all\"\n",
    "\n",
    "            # 在默认浏览器中打开链接\n",
    "            webbrowser.open(url)\n",
    "\n",
    "    # 增加一天\n",
    "    current_date += timedelta(days=1)\n",
    "\n",
    "    # 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\n",
    "    time.sleep(10)\n",
    "\n",
    "# 循环遍历每个子文件夹，将下载好的表格放回相应的子文件夹\n",
    "for subfolder in os.scandir(parent_folder):\n",
    "    if subfolder.is_dir():\n",
    "        # 获取下载好的表格文件\n",
    "        downloaded_files = [f.path for f in os.scandir(download_folder) if f.is_file()]\n",
    "        if downloaded_files:\n",
    "            # 如果有文件，取最新的一个\n",
    "            downloaded_file = max(downloaded_files, key=os.path.getctime)\n",
    "\n",
    "            # 移动下载好的表格到相应的子文件夹\n",
    "            destination_path = os.path.join(subfolder.path, f\"{item_id_variable}_{formatted_date}_data.xlsx\")\n",
    "            shutil.move(downloaded_file, destination_path)\n",
    "            print(f\"Moved file to: {destination_path}\")\n",
    "        else:\n",
    "            print(f\"No files downloaded for item_id {item_id_variable} on {formatted_date}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "022e6855",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品三级流量来源详情-2023-12-26_2023-12-26 (2).xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品三级流量来源详情-2023-12-26_2023-12-26 (2).xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\【生意参谋平台】无线商品三级流量来源详情-2023-12-26_2023-12-26 (2).xls\n"
     ]
    }
   ],
   "source": [
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "import shutil\n",
    "import time\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 26)\n",
    "end_date = datetime(2023, 12, 26)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 循环遍历日期范围\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    # 格式化日期\n",
    "    formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "    # 循环遍历每个子文件夹\n",
    "    for subfolder in os.scandir(parent_folder):\n",
    "        if subfolder.is_dir():\n",
    "            # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "            item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "            # 构建链接\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v3/new/excel/item/source/detail/v3&order=desc&orderBy=uv&dateType=day&dateRange={formatted_date}|{formatted_date}&itemId={item_id_variable}&device=2&pageId=23.s1150&pPageId=30&pageLevel=2&childPageType=se_keyword&belong=all\"\n",
    "\n",
    "            # 在默认浏览器中打开链接\n",
    "            webbrowser.open(url)\n",
    "\n",
    "    # 增加一天\n",
    "    current_date += timedelta(days=1)\n",
    "\n",
    "    # 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\n",
    "    time.sleep(10)\n",
    "\n",
    "# 循环遍历每个子文件夹，将下载好的表格复制到相应的子文件夹\n",
    "for subfolder in os.scandir(parent_folder):\n",
    "    if subfolder.is_dir():\n",
    "        # 获取下载好的表格文件\n",
    "        downloaded_files = [f.path for f in os.scandir(download_folder) if f.is_file()]\n",
    "        if downloaded_files:\n",
    "            # 如果有文件，取最新的一个\n",
    "            downloaded_file = max(downloaded_files, key=os.path.getctime)\n",
    "\n",
    "            # 复制下载好的表格到相应的子文件夹\n",
    "            destination_path = os.path.join(subfolder.path, f\"{os.path.basename(downloaded_file)}\")\n",
    "            shutil.copy(downloaded_file, destination_path)\n",
    "            print(f\"Copied file to: {destination_path}\")\n",
    "        else:\n",
    "            print(f\"No files downloaded for item_id {item_id_variable} on {formatted_date}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d858216a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\743693056409_2023-12-26_data.xlsx\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\743693120439_2023-12-26_data.xlsx\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\744047791585_2023-12-26_data.xlsx\n"
     ]
    }
   ],
   "source": [
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "import shutil\n",
    "import time\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 26)\n",
    "end_date = datetime(2023, 12, 26)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 循环遍历日期范围\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    # 格式化日期\n",
    "    formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "    # 循环遍历每个子文件夹\n",
    "    for subfolder in os.scandir(parent_folder):\n",
    "        if subfolder.is_dir():\n",
    "            # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "            item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "            # 构建链接\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v3/new/excel/item/source/detail/v3&order=desc&orderBy=uv&dateType=day&dateRange={formatted_date}|{formatted_date}&itemId={item_id_variable}&device=2&pageId=23.s1150&pPageId=30&pageLevel=2&childPageType=se_keyword&belong=all\"\n",
    "\n",
    "            # 在默认浏览器中打开链接\n",
    "            webbrowser.open(url)\n",
    "\n",
    "            # 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\n",
    "            time.sleep(10)\n",
    "\n",
    "            # 获取下载好的表格文件\n",
    "            downloaded_files = [f.path for f in os.scandir(download_folder) if f.is_file()]\n",
    "            if downloaded_files:\n",
    "                # 如果有文件，取最新的一个\n",
    "                downloaded_file = max(downloaded_files, key=os.path.getctime)\n",
    "\n",
    "                # 复制下载好的表格到相应的子文件夹\n",
    "                destination_path = os.path.join(subfolder.path, f\"{item_id_variable}_{formatted_date}_data.xlsx\")\n",
    "                shutil.copy(downloaded_file, destination_path)\n",
    "                print(f\"Copied file to: {destination_path}\")\n",
    "            else:\n",
    "                print(f\"No files downloaded for item_id {item_id_variable} on {formatted_date}\")\n",
    "\n",
    "    # 增加一天\n",
    "    current_date += timedelta(days=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "24e4b43a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品三级流量来源详情-2023-12-26_2023-12-26 (8).xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品三级流量来源详情-2023-12-26_2023-12-26 (8).xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\【生意参谋平台】无线商品三级流量来源详情-2023-12-26_2023-12-26 (8).xls\n"
     ]
    }
   ],
   "source": [
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "import shutil\n",
    "import time\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 26)\n",
    "end_date = datetime(2023, 12, 26)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 循环遍历日期范围\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    # 格式化日期\n",
    "    formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "    # 循环遍历每个子文件夹\n",
    "    for subfolder in os.scandir(parent_folder):\n",
    "        if subfolder.is_dir():\n",
    "            # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "            item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "            # 构建链接\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v3/new/excel/item/source/detail/v3&order=desc&orderBy=uv&dateType=day&dateRange={formatted_date}|{formatted_date}&itemId={item_id_variable}&device=2&pageId=23.s1150&pPageId=30&pageLevel=2&childPageType=se_keyword&belong=all\"\n",
    "\n",
    "            # 在默认浏览器中打开链接\n",
    "            webbrowser.open(url)\n",
    "\n",
    "    # 增加一天\n",
    "    current_date += timedelta(days=1)\n",
    "\n",
    "    # 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\n",
    "    time.sleep(10)\n",
    "\n",
    "# 循环遍历每个子文件夹，将下载好的表格复制到相应的子文件夹\n",
    "for subfolder in os.scandir(parent_folder):\n",
    "    if subfolder.is_dir():\n",
    "        # 获取下载好的表格文件\n",
    "        downloaded_files = [f.path for f in os.scandir(download_folder) if f.is_file()]\n",
    "        if downloaded_files:\n",
    "            # 如果有文件，取最新的一个\n",
    "            downloaded_file = max(downloaded_files, key=os.path.getctime)\n",
    "\n",
    "            # 复制下载好的表格到相应的子文件夹\n",
    "            destination_path = os.path.join(subfolder.path, f\"{os.path.basename(downloaded_file)}\")\n",
    "            shutil.copy(downloaded_file, destination_path)\n",
    "            print(f\"Copied file to: {destination_path}\")\n",
    "        else:\n",
    "            print(f\"No files downloaded for item_id {item_id_variable} on {formatted_date}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "aa38519b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品三级流量来源详情-2023-12-26_2023-12-26.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品三级流量来源详情-2023-12-26_2023-12-26 (1).xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\【生意参谋平台】无线商品三级流量来源详情-2023-12-26_2023-12-26 (2).xls\n"
     ]
    }
   ],
   "source": [
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "import shutil\n",
    "import time\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 26)\n",
    "end_date = datetime(2023, 12, 26)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "# 下载路径\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 循环遍历日期范围\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    # 格式化日期\n",
    "    formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "    # 循环遍历每个子文件夹\n",
    "    for subfolder in os.scandir(parent_folder):\n",
    "        if subfolder.is_dir():\n",
    "            # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "            item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "            # 构建链接\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v3/new/excel/item/source/detail/v3&order=desc&orderBy=uv&dateType=day&dateRange={formatted_date}|{formatted_date}&itemId={item_id_variable}&device=2&pageId=23.s1150&pPageId=30&pageLevel=2&childPageType=se_keyword&belong=all\"\n",
    "\n",
    "            # 在默认浏览器中打开链接\n",
    "            webbrowser.open(url)\n",
    "\n",
    "            # 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\n",
    "            time.sleep(10)\n",
    "\n",
    "            # 获取下载好的表格文件\n",
    "            downloaded_files = [f.path for f in os.scandir(download_folder) if f.is_file()]\n",
    "            if downloaded_files:\n",
    "                # 如果有文件，取最新的一个\n",
    "                downloaded_file = max(downloaded_files, key=os.path.getctime)\n",
    "\n",
    "                # 复制下载好的表格到相应的子文件夹\n",
    "                destination_path = os.path.join(subfolder.path, f\"{os.path.basename(downloaded_file)}\")\n",
    "                shutil.copy(downloaded_file, destination_path)\n",
    "                print(f\"Copied file to: {destination_path}\")\n",
    "            else:\n",
    "                print(f\"No files downloaded for item_id {item_id_variable} on {formatted_date}\")\n",
    "\n",
    "    # 增加一天\n",
    "    current_date += timedelta(days=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9c14bd5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品三级流量来源详情-2023-12-12_2023-12-12.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-12_2023-12-12.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品三级流量来源详情-2023-12-12_2023-12-12.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-12_2023-12-12.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\【生意参谋平台】无线商品三级流量来源详情-2023-12-12_2023-12-12.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-12_2023-12-12.xls\n",
      "Time taken for 2023-12-12: 30.091938495635986 seconds\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[18], line 38\u001b[0m\n\u001b[0;32m     35\u001b[0m webbrowser\u001b[38;5;241m.\u001b[39mopen(url)\n\u001b[0;32m     37\u001b[0m \u001b[38;5;66;03m# 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\u001b[39;00m\n\u001b[1;32m---> 38\u001b[0m \u001b[43mtime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msleep\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m     40\u001b[0m \u001b[38;5;66;03m# 获取下载好的表格文件\u001b[39;00m\n\u001b[0;32m     41\u001b[0m downloaded_files \u001b[38;5;241m=\u001b[39m [f\u001b[38;5;241m.\u001b[39mpath \u001b[38;5;28;01mfor\u001b[39;00m f \u001b[38;5;129;01min\u001b[39;00m os\u001b[38;5;241m.\u001b[39mscandir(download_folder) \u001b[38;5;28;01mif\u001b[39;00m f\u001b[38;5;241m.\u001b[39mis_file()]\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "# 手淘流量（三级）\n",
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "import shutil\n",
    "import time\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 26)\n",
    "end_date = datetime(2023, 12, 26)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 循环遍历日期范围\n",
    "current_date = start_date\n",
    "while current_date <= end_date:\n",
    "    # 格式化日期\n",
    "    formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "    # 循环遍历每个子文件夹\n",
    "    for subfolder in os.scandir(parent_folder):\n",
    "        if subfolder.is_dir():\n",
    "            # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "            item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "            # 构建链接\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v3/new/excel/item/source/detail/v3&order=desc&orderBy=uv&dateType=day&dateRange={formatted_date}|{formatted_date}&itemId={item_id_variable}&device=2&pageId=23.s1150&pPageId=30&pageLevel=2&childPageType=se_keyword&belong=all\"\n",
    "\n",
    "            # 在默认浏览器中打开链接\n",
    "            webbrowser.open(url)\n",
    "\n",
    "            # 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\n",
    "            time.sleep(10)\n",
    "\n",
    "            # 获取下载好的表格文件\n",
    "            downloaded_files = [f.path for f in os.scandir(download_folder) if f.is_file()]\n",
    "            if downloaded_files:\n",
    "                # 如果有文件，取最新的一个\n",
    "                downloaded_file = max(downloaded_files, key=os.path.getctime)\n",
    "\n",
    "                # 复制下载好的表格到相应的子文件夹\n",
    "                destination_path = os.path.join(subfolder.path, f\"{os.path.basename(downloaded_file)}\")\n",
    "                shutil.copy(downloaded_file, destination_path)\n",
    "                print(f\"Copied file to: {destination_path}\")\n",
    "\n",
    "                # 删除下载目录中的源文件\n",
    "                os.remove(downloaded_file)\n",
    "                print(f\"Removed source file: {downloaded_file}\")\n",
    "            else:\n",
    "                print(f\"No files downloaded for item_id {item_id_variable} on {formatted_date}\")\n",
    "\n",
    "    # 增加一天\n",
    "    current_date += timedelta(days=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d2fd6eee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品三级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品三级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品三级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品三级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\【生意参谋平台】无线商品三级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\【生意参谋平台】无线商品三级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品三级流量来源详情-2023-12-20_2023-12-20.xls\n"
     ]
    }
   ],
   "source": [
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "import shutil\n",
    "import time\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 18)\n",
    "end_date = datetime(2023, 12, 20)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in os.scandir(parent_folder):\n",
    "    if subfolder.is_dir():\n",
    "        # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "        item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "        # 构建链接列表\n",
    "        urls = []\n",
    "        current_date = start_date\n",
    "        while current_date <= end_date:\n",
    "            formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v3/new/excel/item/source/detail/v3&order=desc&orderBy=uv&dateType=day&dateRange={formatted_date}|{formatted_date}&itemId={item_id_variable}&device=2&pageId=23.s1150&pPageId=30&pageLevel=2&childPageType=se_keyword&belong=all\"\n",
    "            urls.append(url)\n",
    "            current_date += timedelta(days=1)\n",
    "\n",
    "        # 批量打开链接\n",
    "        for url in urls:\n",
    "            webbrowser.open(url)\n",
    "\n",
    "        # 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\n",
    "        time.sleep(5)\n",
    "\n",
    "        # 获取下载好的表格文件\n",
    "        downloaded_files = [f.path for f in os.scandir(download_folder) if f.is_file()]\n",
    "        if downloaded_files:\n",
    "            # 批量复制下载好的表格到相应的子文件夹\n",
    "            for downloaded_file in downloaded_files:\n",
    "                destination_path = os.path.join(subfolder.path, f\"{os.path.basename(downloaded_file)}\")\n",
    "                shutil.copy(downloaded_file, destination_path)\n",
    "                print(f\"Copied file to: {destination_path}\")\n",
    "\n",
    "                # 批量删除下载目录中的源文件\n",
    "                os.remove(downloaded_file)\n",
    "                print(f\"Removed source file: {downloaded_file}\")\n",
    "        else:\n",
    "            print(f\"No files downloaded for item_id {item_id_variable}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4dc10a72",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品二级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品二级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品二级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品二级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693056409\\【生意参谋平台】无线商品二级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品二级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品二级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品二级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品二级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品二级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\743693120439\\【生意参谋平台】无线商品二级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品二级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\【生意参谋平台】无线商品二级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品二级流量来源详情-2023-12-18_2023-12-18.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\【生意参谋平台】无线商品二级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品二级流量来源详情-2023-12-19_2023-12-19.xls\n",
      "Copied file to: D:\\日报（自营 旗舰店）\\水水乐数据源\\旗舰店-单品流量\\744047791585\\【生意参谋平台】无线商品二级流量来源详情-2023-12-20_2023-12-20.xls\n",
      "Removed source file: D:\\新建文件夹\\【生意参谋平台】无线商品二级流量来源详情-2023-12-20_2023-12-20.xls\n"
     ]
    }
   ],
   "source": [
    "import webbrowser\n",
    "from datetime import datetime, timedelta\n",
    "import os\n",
    "import shutil\n",
    "import time\n",
    "\n",
    "# 定义时间段\n",
    "start_date = datetime(2023, 12, 18)\n",
    "end_date = datetime(2023, 12, 20)\n",
    "\n",
    "# 指定包含商品ID子文件夹的父文件夹路径\n",
    "parent_folder = 'D:\\\\日报（自营 旗舰店）\\\\水水乐数据源\\\\旗舰店-单品流量'\n",
    "\n",
    "download_folder = 'D:\\\\新建文件夹'\n",
    "\n",
    "# 循环遍历每个子文件夹\n",
    "for subfolder in os.scandir(parent_folder):\n",
    "    if subfolder.is_dir():\n",
    "        # 从子文件夹路径中提取商品ID（假设子文件夹的名称即为商品ID）\n",
    "        item_id_variable = os.path.basename(subfolder)\n",
    "\n",
    "        # 构建链接列表\n",
    "        urls = []\n",
    "        current_date = start_date\n",
    "        while current_date <= end_date:\n",
    "            formatted_date = current_date.strftime(\"%Y-%m-%d\")\n",
    "            url = f\"https://sycm.taobao.com/flow/excel.do?_path_=v6/excel/item/crowdtype/source/v3&belong=all&dateType=day&dateRange={formatted_date}|{formatted_date}&crowdType=all&device=2&itemId={item_id_variable}&device=2&order=desc&orderBy=uv\"\n",
    "            urls.append(url)\n",
    "            current_date += timedelta(days=1)\n",
    "\n",
    "        # 批量打开链接\n",
    "        for url in urls:\n",
    "            webbrowser.open(url)\n",
    "\n",
    "        # 等待一段时间以确保下载完成，这里可以根据实际情况调整等待时间\n",
    "        time.sleep(5)\n",
    "\n",
    "        # 获取下载好的表格文件\n",
    "        downloaded_files = [f.path for f in os.scandir(download_folder) if f.is_file()]\n",
    "        if downloaded_files:\n",
    "            # 批量复制下载好的表格到相应的子文件夹\n",
    "            for downloaded_file in downloaded_files:\n",
    "                destination_path = os.path.join(subfolder.path, f\"{os.path.basename(downloaded_file)}\")\n",
    "                shutil.copy(downloaded_file, destination_path)\n",
    "                print(f\"Copied file to: {destination_path}\")\n",
    "\n",
    "                # 批量删除下载目录中的源文件\n",
    "                os.remove(downloaded_file)\n",
    "                print(f\"Removed source file: {downloaded_file}\")\n",
    "        else:\n",
    "            print(f\"No files downloaded for item_id {item_id_variable}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ffcc715",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
